<?php


namespace app\index\controller\wanlshop;

use app\common\controller\Backend;
use app\common\controller\Wanlshop;
use think\Exception;

/**
 * 店铺结算
 *
 * @icon fa fa-circle-o
 */
class Settlement extends Wanlshop
{

    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    /**
     * Settlement模型对象
     * @var \app\common\model\wanlshop\Settlement
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\wanlshop\Settlement;
        $this->view->assign("wayList", $this->model->getWayList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['admin','shop','package'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->getRelation('admin')->visible(['nickname']);
                $v->getRelation('shop')->visible(['shopname']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    //详情
    public function detail($ids){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $package = model('app\api\model\wanlshop\Package')
            ->where('package_no', 'in', $row['package_no'])
            ->find();
        if(!$package){
            $this->error(__('订单异常'));
        }
        $package['goods'] = model('app\api\model\wanlshop\Packagegoods')
            ->where(['package_id' => $package['id']])
            ->select();
        $this->view->assign('row',$row);
        $this->view->assign('package',$package);
        return $this->view->fetch();
    }
}
